// 上传 oss minxins 封装
export default {
	components: {},
	data() {
		return {};
	},
	onLoad() {},
	methods: {
		uploadUserImage(path, ossFileType, fileName, familyId, callback) {

			let query = {
				familyId: '',
				uploadUserImage: true,
				ossFileType
			}
			
			this.$api.getOssPolicy(query).then(res => {
				let params = {
					'OSSAccessKeyId': res.accessKeyId,
					'policy': res.policy,
					'signature': res.signature,
					'key': res.dir + fileName,
					'success_action_status': '200',
					'familyId': familyId
				}
				console.log('path',path)
				console.log('res.host',res.host)
				console.log('params', params)
				uni.showLoading({
					title: '上传中,请耐心等待,不要离开当前页...'
				})
				uni.uploadFile({
					url: res.host,
					filePath: path, // 文件
					name: 'file', // 在FormData 中文件对应的属性名
					formData: params, // 除文件外其他所有数据，传对象，会默认转换为 FormData
					header: {
						'token': uni.getStorageSync('token') ? uni.getStorageSync('token') : ''
					},
					success: reselt => {
						console.log(reselt)
						if (reselt.statusCode == 200) {
							let imagePath = res.host + '/' + res.dir + fileName
							// 上传服务器
							console.log('upload image path: '+imagePath);
							callback(imagePath)
							uni.hideLoading()
						} else {
							this.$Show.show('上传失败')
							uni.hideLoading()
						}
					},
					fail: error => {
						console.log(error)
					}
				})

			})
		},
		uploadUserImage2(path, ossFileType, fileName, familyId, callback) {

			let query = {
				ossFileType,
				uploadUserImage: true
			}
			console.log(path)
			this.$api.getOssUserAvatarPolicy(query).then(res => {
				console.log(path)
				let params = {
					'OSSAccessKeyId': res.accessKeyId,
					'policy': res.policy,
					'signature': res.signature,
					'key': res.dir + fileName,
					'success_action_status': '200',
					'familyId': familyId
				}
				console.log(params)
				uni.showLoading({
					title: '上传中...'
				})
				uni.uploadFile({
					url: res.host,
					filePath: path, // 文件
					name: 'file', // 在FormData 中文件对应的属性名
					formData: params, // 除文件外其他所有数据，传对象，会默认转换为 FormData
					header: {
						'token': uni.getStorageSync('token') ? uni.getStorageSync('token') : ''
					},
					success: reselt => {
						uni.hideLoading()
						if (reselt.statusCode == 200) {
							let imagePath = res.host + '/' + res.dir + fileName
							// 上传服务器

							callback(imagePath)
						} else {
							this.$Show.show('上传失败')
						}
						console.log(reselt)
					},
				})

			})
		},
		// 压缩图片
		compressImageByPath(filePath, fileName, maxWidth, maxHeight, quality) {
			return new Promise((resolve, reject) => {
				uni.getImageInfo({
					src: filePath,
					success: (imageInfo) => {
						const {
							width,
							height,
							path
						} = imageInfo;

						// 计算缩放比例
						let scale = 1;
						if (width > maxWidth || height > maxHeight) {
							if (width / maxWidth > height / maxHeight) {
								scale = maxWidth / width;
							} else {
								scale = maxHeight / height;
							}
						}

						const canvasWidth = width * scale;
						const canvasHeight = height * scale;

						// 创建canvas
						const canvas = document.createElement('canvas');
						const ctx = canvas.getContext('2d');
						canvas.width = canvasWidth;
						canvas.height = canvasHeight;

						// 使用uni-app的canvas API绘制图片
						const img = new Image();
						img.onload = () => {
							ctx.drawImage(img, 0, 0, canvasWidth, canvasHeight);

							// 转换为Blob对象
							canvas.toBlob((blob) => {
								const imgSrc = window.URL.createObjectURL(blob);
								resolve({
									imgSrc,
									fileName
								});
							}, 'image/jpeg', quality);

							// canvas.toBlob(function(fileSrc) {
							// 	let imgSrc = window.URL.createObjectURL(fileSrc);
							// 	console.log('压缩后', imgSrc, fileName);
							// 	resolve({
							// 		imgSrc,
							// 		fileName
							// 	});
							// });
						};
						img.onerror = (error) => {
							reject(error);
						};
						img.src = path;
					},
					fail: (error) => {
						reject(error);
					}
				});
			});
		},
		getImageInfo2(src, fileName, maxWidth, maxHeight, quality) {
			console.log('getImageInfo', src, fileName);
			let _this = this;
			return new Promise((resolve, reject) => {
		
				uni.getImageInfo({
					src: src,
					success(res) {
						console.log('压缩前', res);
						let width = res.width; //图片原始长宽
						let height = res.height;
						let img = new Image();
						img.src = res.path;
						let canvas = document.createElement('canvas');
						let ctx = canvas.getContext('2d');
						// 计算缩放比例
						let scale = 1;
						if (width > maxWidth || height > maxHeight) {
							if (width / maxWidth > height / maxHeight) {
								scale = maxWidth / width;
							} else {
								scale = maxHeight / height;
							}
						}
						canvas.width = width * scale;
						canvas.height = height * scale;
						console.log(img);
						ctx.drawImage(img, 0, 0, canvas.width, canvas.height);
						canvas.toBlob(function(fileSrc) {
							let imgSrc = window.URL.createObjectURL(fileSrc);
							console.log('压缩后', imgSrc, fileName);
							resolve({
								imgSrc,
								fileName
							});
						}, 'image/jpeg', quality);
					}
				});
			});
		},
		getImageInfo(src, fileName) {
			console.log('getImageInfo', src, fileName);
			let _this = this;
			return new Promise((resolve, reject) => {

				uni.getImageInfo({
					src: src,
					success(res) {
						console.log('压缩前', res);
						let canvasWidth = res.width; //图片原始长宽
						let canvasHeight = res.height;
						let img = new Image();
						img.src = res.path;
						let canvas = document.createElement('canvas');
						let ctx = canvas.getContext('2d');
						canvas.width = canvasWidth / 2;
						canvas.height = canvasHeight / 2;
						console.log(img);
						ctx.drawImage(img, 0, 0, canvasWidth / 2, canvasHeight / 2);
						canvas.toBlob(function(fileSrc) {
							let imgSrc = window.URL.createObjectURL(fileSrc);
							console.log('压缩后', imgSrc, fileName);
							resolve({
								imgSrc,
								fileName
							});
						});
					}
				});
			});
		},
	}
};